﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.IO;
using System.Windows.Forms;
using DAL;
using Entities;

namespace BL
{
    public class StockMovBLL 
    { 
        public static DataSet dt;
        public DataTable tblStockMov;
        public DataTable tblStockMovDetalle;

        public static DataSet CrearDataset()
        {
            string strFilePath = Application.StartupPath + "\\Datasets_xml\\";
            if (File.Exists(strFilePath + "StockMov.xml"))
            {
                dt = new DataSet();
                dt.ReadXml(strFilePath + "StockMov.xml");
                dt.AcceptChanges();
                return dt;
            }
            else
            {
                DataSet dt = DAL.StockMovDAL.CrearDataset();
                dt.Tables[0].TableName = "StockMov";
                dt.Tables[1].TableName = "StockMovDetalle";

                DataRelation stockMovimientosDetalle = dt.Relations.Add(
                    "StockMovimientosDetalle", dt.Tables["StockMov"].Columns["IdMovMSTK"],
                    dt.Tables["StockMovDetalle"].Columns["IdMovMSTKD"]);
                stockMovimientosDetalle.Nested = true;
                dt.WriteXml(strFilePath + "StockMov.xml", XmlWriteMode.WriteSchema);
                return dt;
            }
        }

        public static DataSet CrearDatasetCons(string fechaDesde, string fechaHasta, int idLocal, 
                string tipoMov, string movimiento, ref int? codigoError)
        {
            try
            {
                string strFilePath = Application.StartupPath + "\\Datasets_xml\\";
                dt = DAL.StockMovDAL.CrearDatasetCons(fechaDesde, fechaHasta, idLocal, tipoMov, movimiento);
                dt.Tables[0].TableName = "StockMov";
                dt.Tables[1].TableName = "StockMovDetalle";
                dt.Tables[1].Columns.Remove("FechaMSTK");
                dt.Tables[1].Columns.Remove("OrigenMSTK");
                dt.Tables[1].Columns.Remove("DestinoMSTK");
                dt.Tables[1].Columns.Remove("CompensaMSTK");

                DataRelation stockMovDetalle = dt.Relations.Add(
                    "StockMovDetalle", dt.Tables["StockMov"].Columns["IdMovMSTK"],
                    dt.Tables["StockMovDetalle"].Columns["IdMovMSTKD"]);
                stockMovDetalle.Nested = true;
                dt.WriteXml(strFilePath + "StockMov.xml", XmlWriteMode.WriteSchema);
            }
            catch (MySqlException ex)
            {
                if (ex.Number == 1042) //no se pudo abrir la conexion por falta de internet
                {
                    codigoError = 1042;
                }
                else
                {
                    codigoError = ex.Number;
                }
            }
            return dt;
        }

        public static void InsertarDT(DataTable tabla, StockMov entidad)
        {
            DAL.StockMovDAL.InsertarDT(tabla, entidad);
        }

        public static void EditarDT(DataRowView vistaFilaActual, StockMov entidad)
        {
            DAL.StockMovDAL.EditarDT(vistaFilaActual, entidad);
        }

        public static void EditarDT(string idArticulo, StockMov entidad, DataTable tbl, DataSet dt)
        {
            DAL.StockMovDAL.EditarDT(idArticulo, tbl, entidad, dt);
        }

        public static void BorrarDT(DataGridView gvwDatos, string origen, DataSet dt)
        {
            DAL.StockMovDAL.BorrarDT(gvwDatos, origen, dt);
        }

        public static void GrabarDB(DataSet dt)
        {
            MySqlTransaction tr = null;
            try
            {
                MySqlConnection SqlConnection1 = DALBase.GetConnection();
                SqlConnection1.Open();
                tr = SqlConnection1.BeginTransaction();
       //         DAL.StockMovDAL.GrabarDB(dt, SqlConnection1, tr);
         //       string strFilePath = Application.StartupPath + "\\Datasets_xml\\";
                string strFilePath = Application.StartupPath + "\\Datasets_xml\\";
                dt.WriteXml(strFilePath + "StockMov.xml", XmlWriteMode.WriteSchema);
                tr.Commit();
                SqlConnection1.Close();
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.ToString(), "NcSoft", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dt.RejectChanges();
                tr.Rollback();
            }
        }

        public static void BorrarByPK(int PK, ref int? codigoError)
        {
            try
            {
                DAL.StockMovDAL.BorrarByPK(PK);
            }
            catch (MySqlException ex)
            {
                if (ex.Number == 1042) //no se pudo abrir la conexion por falta de internet
                {
                    codigoError = 1042;
                }
                else
                {
                    codigoError = ex.Number;
                }
            }
        }
    }
}
